package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.annotation.GuiGuLogin;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.model.album.TrackStat;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;

	@Autowired
	private VodService vodService;

	@Autowired
	private RedisTemplate redisTemplate;


	//腾讯云上传声音
	// 声音是文件  这个文件除了进行存储之外还需要进行播放   要把声音文件上传到腾讯云里面
	// 在腾讯云里面有专门的对视频音频处理的服务  称为：云点播服务
	//在腾讯云云点播服务里面  实现声音文件存储  声音文件播放
	@Operation(summary = "上传声音")
	@PostMapping("/uploadTrack")
	public Result<Map<String, Object>> uploadTrack(MultipartFile file){
		Map<String,Object> map = vodService.uploadTrack(file);
		return Result.ok(map);
	}
	//新增声音（提交声音）
	@Operation(summary = "新增声音")
	@PostMapping("/saveTrackInfo")
	public Result savaTrackInfo(@RequestBody @Validated TrackInfoVo trackInfoVo){
		Long userId = 1L;
		trackInfoService.saveTrackInfo(trackInfoVo,userId);
		return Result.ok();
	}
	//查询声音列表
	@Operation(summary = "分页查询声音列表")
	@PostMapping("/findUserTrackPage/{page}/{limit}")

	public Result findUserTrackPage(@PathVariable(value = "page") Long page,
									@PathVariable(value = "limit") Long limit,
									@RequestBody TrackInfoQuery trackInfoQuery,
									HttpServletRequest request){
		//1 从请求头中获取token token是否存在
		String token = request.getHeader("token");
		//如果token为空表示没有登录，要跳转到登录页面
		if(StringUtils.isEmpty(token)){
			//表示没有登录
			throw new GuiguException(ResultCodeEnum.LOGIN_AUTH);
		}
		//根据token查询redis  是否可以查到数据
		UserInfo userInfo = (UserInfo) redisTemplate.opsForValue().get(token);
		if(userInfo==null){
			//表示没有登录
			throw new GuiguException(ResultCodeEnum.LOGIN_AUTH);
		}

		trackInfoQuery.setUserId(1L);
		Page<TrackListVo> trackInfoPage = new Page<>(page,limit);
		IPage<TrackListVo> trackListVoIPage = trackInfoService.findUserTrackPage(trackInfoPage,trackInfoQuery);
		return Result.ok(trackListVoIPage);
	}

	//根据id删除声音
	@Operation(summary = "删除声音")
	@DeleteMapping("/removeTrackInfo/{id}")
	public Result removeTrackInfo(@PathVariable(value = "id") Long id){
		trackInfoService.removeTrackInfo(id);
		return Result.ok();
	}

	//修改声音之回显数据（根据id查询数据）
	@Operation(summary = "获取声音信息")
	@GetMapping("/getTrackInfo/{id}")
	public Result getTrackInfo(@PathVariable(value = "id") Long id){
		TrackInfo trackInfo =  trackInfoService.getTrackInfo(id);
		return Result.ok(trackInfo);
	}

	//修改声音数据
	@Operation(summary = "修改声音")
	@PutMapping("/updateTrackInfo/{id}")
	public Result updateTrackInfo(@PathVariable(value = "id") Long id,
								  @RequestBody  @Validated TrackInfoVo trackInfoVo){
		trackInfoService.updateTrackInfo(id,trackInfoVo);
		return Result.ok();
	}

	//根据专辑id分页获取声音信息
	@GuiGuLogin (required = false)
	@Operation(summary = "获取专辑声音分页列表")
	@GetMapping("findAlbumTrackPage/{albumId}/{page}/{limit}")
	public Result<IPage<AlbumTrackListVo>> findAlbumTrackPage(@PathVariable Long albumId,
															  @PathVariable Long page,
															  @PathVariable Long limit) {
		//获取userId
		Long userId = AuthContextHolder.getUserId();
		Page<AlbumTrackListVo> listVoPage = new Page<>(page,limit);
		IPage<AlbumTrackListVo> page1 = trackInfoService.findAlbumTrackPage(listVoPage,albumId,userId);
		return 	Result.ok(page1);
	}

	//获取购买声音的方式页面(本集  前10集)
	@GuiGuLogin
	@Operation(summary = "购买声音方式")
	@GetMapping("/findUserTrackPaidList/{trackId}")
	public Result<List<Map<String,Object>>> findUserTrackPaidList(@PathVariable(value = "trackId") Long trackId){
		//根据声音id获取购买声音的方式
		List<Map<String,Object>> mapList = 	trackInfoService.findUserTrackPaidList(trackId);
		return Result.ok(mapList);
	}
	//查询当前购买声音的数据列表
	@GuiGuLogin
	@Operation(summary = "查询当前购买声音的数据列表")
	@GetMapping("/findPaidTrackInfoList/{itemId}/{trackCount}")
	Result<List<TrackInfo>> findPaidTrackInfoList(@PathVariable(value = "itemId") Long itemId,
											  @PathVariable(value = "trackCount") Integer trackCount){
	List<TrackInfo>	trackInfoList = trackInfoService.findPaidTrackInfoList(itemId,trackCount);
	return Result.ok(trackInfoList);
	}

//	//根据声音id获取声音数据
//	@PostMapping("/api/album/trackInfo/getTrackInfo")
//	Result<TrackInfo> getTrackInfo(@PathVariable("trackId") Long trackId)
//	{
//		TrackInfo trackInfo = trackInfoService.getTrackInfo(trackId);
//		return Result.ok(trackInfo);
//	}


}

